<template>
	<view class="w-picker-view">
		<picker-view class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
			<picker-view-column>
				<view class="w-picker-item" v-for="(item,index) in range.years" :key="index">{{item}}年</view>
			</picker-view-column>
			<picker-view-column>
				<view class="w-picker-item" v-for="(item,index) in range.months" :key="index">{{item}}月</view>
			</picker-view-column>
			<picker-view-column>
				<view class="w-picker-item" v-for="(item,index) in range.weeks" :key="index">第{{item}}周</view>
			</picker-view-column>
		</picker-view>
		<!-- 		<u-select v-model="show" mode="mutil-column-auto" :list="list"></u-select> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pickVal: [],
				range: {},
				checkObj: {},
				list: [],
				show: true
			};
		},
		props: {
			itemHeight: {
				type: String,
				default: "44px"
			},
			startYear: {
				type: String,
				default: ""
			},
			endYear: {
				type: String,
				default: ""
			},
			value: {
				type: [String, Array, Number],
				default: ""
			},
			current: { //是否默认选中当前日期
				type: Boolean,
				default: false
			},
		},
		watch: {
			// value(val){
			// 	console.log('watch',val)
			// 	this.initData();
			// }
		},
		created() {
			this.initData();
		},
		methods: {
			//一月中的第几周
			getMonthWeekCur(a, b, c) {
				let date = new Date(a, parseInt(b) - 1, c),
					w = date.getDay(),
					d = date.getDate();
				return Math.ceil(
					(d + 6 - w) / 7
				);
			},
			getWeekNum(year, month) {
				let d = new Date()
				d.setFullYear(year, month, 0)
				let monthDays = d.getDate() // 该月天数
				let weeks = 0 // 该月周数
				// 计算第一周
				d.setFullYear(year, month - 1, 1) // 该月第一天
				let w1 = d.getDay() // 获取星期几(0~6) 该月第一天 星期几
				// 按照星期一到星期天的形式计算
				if (w1 === 0) {
					// 如果这个月的一号是这周的最后一天
					// weeks = 1
					weeks = 0
					monthDays = monthDays - 1
				} else {
					// 如果这个月的一号不是这周的最后一天
					weeks = 1
					monthDays = monthDays - (7 - w1 + 1)
				}
				// 计算最后一周天数
				d.setFullYear(year, month, 0)
				const newmonthDays = d.getDate()
				d.setFullYear(year, month - 1, newmonthDays) // 该月第最后一天
				const w2 = d.getDay() // 获取星期几(0~6) 该月第一天 星期几
				if (w2 === 0) {
					monthDays = monthDays - 7
				} else {
					monthDays = monthDays - w2
				}
				weeks = weeks + 1 // 再加最后一周
				weeks = weeks + (monthDays / 7) // 加上剩余的周数
				return weeks
			},
			formatNum(n) {
				return (Number(n) < 10 ? '0' + Number(n) : Number(n) + '');
			},
			checkValue(value) {
				let strReg = /^\d{4}-\d{2}-\d{2}$/,
					example;
				if (!strReg.test(value)) {
					console.log(new Error("请传入与mode、fields匹配的value值，例value=" + example + ""))
				}
				return strReg.test(value);
			},
			getData(dVal) {
				//用来处理初始化数据
				console.log('处理数据', dVal)
				let years = [],
					months = [],
					weeks = [];
				let startYear = this.getStartDate().getFullYear();
				let endYear = this.getEndDate().getFullYear();
				let year = dVal[0] * 1;
				let month = dVal[1] * 1;
				let weekLen = this.getWeekNum(year, month);
				for (let year = startYear; year <= endYear; year++) {
					years.push(year.toString())
				}
				for (let month = 1; month <= 12; month++) {
					months.push(this.formatNum(month));
				}
				for (let week = 1; week <= weekLen; week++) {
					weeks.push(week);
				}
				return {
					years,
					months,
					weeks
				}
			},
			getCurrenDate() {
				let curDate = new Date();
				let curYear = curDate.getFullYear();
				let curMonth = curDate.getMonth() + 1;
				let curMonthdays = new Date(curYear, curMonth, 0).getDate();
				let curDay = curDate.getDate();

				let curWeek = this.getMonthWeekCur(curYear, curMonth, curDay);
				console.log('curWeek', curWeek)

				return {
					curDate,
					curYear,
					curMonth,
					curMonthdays,
					curWeek
				}
			},
			getDefaultDate() {
				let value = this.value;
				let reg = /-/g;
				let defaultDate = value ? new Date(value.split(" ")[0].replace(reg, "/")) : new Date();
				let defaultYear = defaultDate.getFullYear();
				let defaultMonth = defaultDate.getMonth() + 1;
				let defaultDay = defaultDate.getDate();
				return {
					defaultDate,
					defaultYear,
					defaultMonth,
					defaultDay,
				}
			},
			getStartDate() {
				let start = this.startYear;
				let startDate = "";
				let reg = /-/g;
				if (start) {
					startDate = new Date(start + "/01/01");
				} else {
					startDate = new Date("1970/01/01");
				}
				return startDate;
			},
			getEndDate() {
				let end = this.endYear;
				let reg = /-/g;
				let endDate = "";
				if (end) {
					endDate = new Date(end + "/12/31");
				} else {
					endDate = new Date();
				}
				return endDate;
			},
			getDval() {
				let value = this.value;
				let dVal = null;
				let aDate = new Date();
				let year = this.formatNum(aDate.getFullYear());
				let month = this.formatNum(aDate.getMonth() + 1);
				let day = this.formatNum(aDate.getDate());
				let week = this.getMonthWeekCur(year, month, day);
				if (value) {
					let flag = this.checkValue(value);
					if (!flag) {
						dVal = [year, month, week]
					} else {
						let v = value.split(" ");
						let valList = v[0].split('-');
						week = this.getMonthWeekCur(valList[0], valList[1], valList[2]);
						dVal = [valList[0], valList[1], week];
					}
				} else {
					dVal = [year, month, week]
				}
				return dVal;
			},
			initData() {
				console.log('init')
				let years = [],
					months = [],
					weeks = [];
				let dVal = this.getDval();
				let dateData = this.getData(dVal);
				years = dateData.years;
				months = dateData.months;
				weeks = dateData.weeks;
				this.range = {
					years,
					months,
					weeks
				};
				let pickVal = [
					dVal[0] && years.indexOf(dVal[0]) != -1 ? years.indexOf(dVal[0]) : 0,
					dVal[1] && months.indexOf(dVal[1]) != -1 ? months.indexOf(dVal[1]) : 0,
					dVal[2] && weeks.indexOf(dVal[2]) != -1 ? weeks.indexOf(dVal[2]) : 0,
				];
				this.$nextTick(() => {
					this.pickVal = pickVal;
				});
				let year = dVal[0] ? dVal[0] : years[0];
				let month = dVal[1] ? dVal[1] : months[0];
				let week = dVal[2] ? dVal[2] : weeks[0];

				let result = "",
					full = "",
					obj = {};
				result = full = `${year+'-'+month+' '+week}`;
				obj = {
					year,
					month,
					week,
				}

				this.checkObj = obj;

				this.$emit("change", {
					result: result,
					value: full,
					obj: obj
				})

			},
			handlerChange(e) {
				let arr = [...e.detail.value];
				let data = this.range;
				let year = "",
					month = "",
					week = "";
				year = (arr[0] || arr[0] == 0) ? data.years[arr[0]] || data.years[data.years.length - 1] : "";
				month = (arr[1] || arr[1] == 0) ? data.months[arr[1]] || data.months[data.months.length - 1] : "";
				week = (arr[2] || arr[2] == 0) ? data.weeks[arr[2]] || data.weeks[data.weeks.length - 1] : "";
				let pickVal = this.pickVal;
				let weeks = [];
				if (pickVal[0] != arr[0] || pickVal[1] != arr[1]) {
					let weekLen = this.getWeekNum(year, month);
					if (data.weeks.length != weekLen) {
						for (let week = 1; week <= weekLen; week++) {
							weeks.push(week);
						}
						this.range.weeks = weeks;
					}
				}
				let result = "",
					full = "",
					obj = {};
				result = full = `${year+'-'+month+' '+week}`;
				obj = {
					year,
					month,
					week,
				}

				this.checkObj = obj;
				this.pickVal = arr;

				this.$emit("change", {
					result: result,
					value: full,
					obj: obj
				})

			},

		}
	}
</script>

<style lang="scss">
	@import "./w-picker.css";
</style>